﻿<template>
  <l-layout class="l-tab-page" v-show="!lr_loadPage">
    <l-panel v-if="showView" title="物料清单">
      <template #toolLeft>
        <l-query2
          :items="queryItems"
          :formData="queryData"
          :loading="tableLoading"
          @search="handleSearch"
        >
          <template #f_Type>
            <l-select :options="f_TypeOptions" v-model="queryData.f_Type" />
          </template>
          <template #f_Property>
            <l-select
              :options="f_PropertyOptions"
              v-model="queryData.f_Property"
            />
          </template>
          <template #f_State>
            <l-select
              :options="f_StateOptions"
              v-model="queryData.f_State"
            ></l-select>
          </template>
          <template #f_Name>
            <el-input v-model="queryData.f_Name" :placeholder="$t('please enter')" />
          </template>
        </l-query2>
      </template>
      <template #toolRight>
        <l-tool-btns
          :hasTableSetting="true"
          @setting="lr_handleSettingTable"
          @click="handleAdd()"
        >
          <l-excel-btns></l-excel-btns>
        </l-tool-btns>
      </template>
      <l-table
        ref="mainTable"
        :loading="tableLoading"
        :columns="lr_getPageColumns(columns)"
        :dataSource="tableData"
        :isPage="true"
        :pageTotal="tableTotal"
        :tablePage.sync="tableCurrentPage"
        @loadPageData="turnTablePage"
        rowKey="f_Id"
        isMultiSelect
        @selectionChange="selectChange"
      >
        <template
          v-for="(col, index) in lr_tableColumnsByToLabel(
            lr_getPageColumns(columns)
          )"
          v-slot:[col.prop]="scope"
        >
          <l-value-to-label
            :value="scope.row[col.prop]"
            :type="col.dataType"
            :code="col.dataCode"
            :valueKey="col.valueKey"
            :labelKey="col.labelKey"
            :options="col.options"
            :format="col.format"
            :setting="col.setting"
            :key="index"
          ></l-value-to-label>
        </template>

        <template v-slot:f_State="scope">
          <el-switch
            :active-value="0"
            :inactive-value="1"
            v-model="scope.row.f_State"
            @change="changeState(scope.row)"
          >
          </el-switch>
        </template>
        <l-table-btns
          :btns="tableBtns"
          @click="lr_handleTableBtnClick"
        ></l-table-btns>
      </l-table>
    </l-panel>

    <transition name="el-zoom-in-center">
      <l-panel v-if="!showView">
        <template #title>
          <el-page-header @back="showView = true" content="物料Details查看" />
        </template>
        <view-form :row="formEditRow"></view-form>
      </l-panel>
    </transition>

    <!--表单-->
    <l-dialog
      :title="$t(formTitle)"
      :visible.sync="formVisible"
      :height="480"
      :width="600"
      @ok="handleSave"
      @close="handleCloseForm"
      @opened="handleOpenedForm"
    >
      <my-form
        ref="form"
        :isReadOnly="formDetails"
        v-if="formVisible"
      ></my-form>
    </l-dialog>
  </l-layout>
</template>

<script>
import MyForm from "./form"; // Add/Edit表单
import viewForm from "./viewForm.vue"; // 查看表单
const api = window.$api.erpcase.material;
export default {
  components: {
    MyForm,
    viewForm,
  },

  data() {
    return {
      lr_isPage: true,
      moduleCode: "ERP_Materials",

      // query参数
      queryData: {
        f_Type: "",
        f_Property: "",
        f_Name: "",
        f_State: "",
      },

      queryItems: [
        { label: "物料类别", prop: "f_Type" },
        { label: "物料属性", prop: "f_Property" },
        { label: "物料状态", prop: "f_State" },
        { label: "物料名称", prop: "f_Name" },
      ],

      f_StateOptions: [
        {
          label: "已启用",
          value: 0,
        },
        {
          label: "未启用",
          value: 1,
        },
        {
          label: "全部",
          value: null,
        },
      ],

      // 表格参数
      tableLoading: false,
      tableData: [],
      tableTotal: 0,
      tableCurrentPage: 1,
      tablePageSize: 50,
      columns: [
        {
          label: "物料编码",
          prop: "f_Number",
          minWidth: 120,
          align: "left",
          dataType: "guid",
          isNotAutoWrap: true,
        },
        {
          label: "物料名称",
          prop: "f_Name",
          minWidth: 120,
          align: "left",
          isNotAutoWrap: true,
        },
        {
          label: "规格型号",
          prop: "f_Model",
          minWidth: 120,
          align: "left",
        },
        {
          label: "单位",
          prop: "f_Unit",
          minWidth: 64,
          align: "center",
          dataType: "dataSource",
          dataCode: "ERP_Unit",
          valueKey: "f_id",
          labelKey: "f_name",
        },
        {
          label: "物料类别",
          prop: "f_Type",
          minWidth: 100,
          align: "left",
          dataType: "dataSource",
          dataCode: "ERP_MaterialClasses",
          valueKey: "f_id",
          labelKey: "f_type",
        },
        {
          label: "物料属性",
          prop: "f_Property",
          minWidth: 100,
          align: "left",
          dataType: "dataSource",
          dataCode: "ERP_MaterialProp",
          valueKey: "f_id",
          labelKey: "f_type",
        },
        { label: "状态", prop: "f_State", minWidth: 80, align: "center" },
      ],
      tableBtns: [
        { prop: "Edit", label: "Edit",width:40 },
        { prop: "Delete", label: "Delete",width:40 },
        { prop: "View", label: "查看",width:40 },
      ],

      // 表单参数
      formVisible: false,
      formTitle: "",
      formEditRow: null,
      formEdit: false,
      formDetails: false,

      // 查看表单参数
      showView: true,

      tableSelection: [], // 表中的数据
    };
  },
  computed: {
    f_TypeOptions() {
      const data = this.lr_DataSource("ERP_MaterialClasses");
      return this.lr_DataSourceOptions(data, "f_id", "f_type");
    },
    f_PropertyOptions() {
      const data = this.lr_DataSource("ERP_MaterialProp");
      return this.lr_DataSourceOptions(data, "f_id", "f_type");
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      this.loadTableData();
    },
    handleSearch() {
      this.loadTableData();
    },
    turnTablePage({ rows }) {
      this.tablePageSize = rows;
      this.loadTableData(true);
    },
    async loadTableData(isNotFirst) {
      if (!isNotFirst) {
        this.tableCurrentPage = 1;
      }
      this.tableLoading = true;
      let queryData = this.$deepClone(this.queryData);

      queryData.rows = this.tablePageSize;
      queryData.page = this.tableCurrentPage;
      queryData.sidx = "f_Number DESC";
      let data = await this.$awaitWraper(api.getPage(queryData));
      if (data != null) {
        this.tableData = data.rows;
        this.tableTotal = data.records;
      } else {
        this.tableData = [];
      }
      this.tableLoading = false;
    },

    selectChange(val) {
      this.tableSelection = val;
    },

    handleAdd() {
      this.formEdit = false;
      this.formDetails = false;
      this.handleShowForm("Add物料清单");
    },
    handleEdit($index, row) {
      this.formEdit = true;
      this.formDetails = false;
      this.formEditRow = row;
      this.handleShowForm("Edit物料清单");
    },
    handleDelete($index, row) {
      this.$confirm(
        this.$t("This operation will permanently delete the data. Do you want to continue?"),
        this.$t("Tips"),
        {
          confirmButtonText: this.$t("confirm"),
          cancelButtonText: this.$t("cancel"),
          type: "warning",
        }
      )
        .then(async () => {
          this.tableData.splice($index, 1);
          this.tableTotal--;
          await api.remove(row.f_Id);
          this.$message({
            type: "success",
            message: this.$t("Delete  success"),
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: this.$t("Delete has been canceled!"),
          });
        });
    },

    async handleSave(showLoading, hideLoading) {
      showLoading("saving data...");
      if (await this.$refs.form.validateForm()) {
        const res = await this.$refs.form.saveForm({
          keyValue: this.formEdit ? this.formEditRow.f_Id : "",
          isEdit: this.formEdit,
        });
        if (res) {
          const msg = this.formEdit ? "更新成功!" : "Add成功!";
          this.$message({
            type: "success",
            message: this.$t(msg),
          });
          this.loadTableData();
          this.formVisible = false;
        }
      }
      hideLoading();
    },

    handleShowForm(text) {
      this.formTitle = text;
      this.formVisible = true;
    },
    async handleOpenedForm(showLoading, hideLoading) {
      if (this.formEdit) {
        showLoading("Loading data...");
        await this.$refs.form.loadFormData({ keyValue: this.formEditRow.f_Id });
        hideLoading();
      }
    },
    handleCloseForm() {
      this.$refs.form.resetForm();
    },

    // 修改列表页的状态
    async changeState(row) {
      if (row.f_State == 0) {
        let queryData = {
          f_State: "0",
          f_Id: row.f_Id,
        };
        let data = await this.$awaitWraper(
          api.changeState(row.f_Id, queryData)
        );
        if (data) {
          this.$message({
            type: "success",
            message: data,
          });
        }
      } else if (row.f_State == 1) {
        let queryData = {
          f_State: "1",
          f_Id: row.f_Id,
        };
        let data = await this.$awaitWraper(
          api.changeState(row.f_Id, queryData)
        );
        if (data) {
          this.$message({
            type: "success",
            message: data,
          });
        }
      }
    },

    handleView($index, row) {
      this.showView = false;
      this.formEditRow = row;
    },
  },
};
</script>
